#include "myscheduletable.h"
#include "global.h"
#include <QHeaderView>

MyScheduleTable::MyScheduleTable(QWidget *parent) : MyTableWidget(parent)
{
    setParent(parent);
}

void MyScheduleTable::setScheduleHorizontalHeaderStyle(QFont headerFont, QString styleSheet, bool headFixed)
{
    setHorizontalHeaderStyle(headerFont, styleSheet, headFixed);

     // 随文字变更表头大小
    horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    return;
}

void MyScheduleTable::setScheduleTableWidgetStyle()
{
    // 不显示行号
    verticalHeader()->setVisible(true);

    setTableWidgetStyle();
    return;
}

void MyScheduleTable::createItemAndSetStyle(int rowpos, int rowcount, int colpos, int colcount)
{
    Qt::ItemFlags flag;
    QBrush backgroundBrush;

    for(int i = 0; i < this->rowCount(); i++){
        for(int j = 0; j < this->columnCount(); j++){
            QTableWidgetItem *pItem = createItem("", i, j);
            if(NULL == pItem)   continue;

            // 特殊行 "个人白班小计", "个人夜班小计", "个人班时总计"
            if(i >= rowpos && i < rowpos + rowcount && j < colpos)
                backgroundBrush = SCHEDULE_TABLE_SPECIFY_ITEM_COLOR;
            // 特殊列 "白班人数", "夜班人数", "总人数"
            else if(j >= colpos && j < colpos + colcount - 1 && i < rowpos)
                backgroundBrush = SCHEDULE_TABLE_SPECIFY_ITEM_COLOR;
            else
                backgroundBrush = SCHEDULE_TABLE_COMMON_ITEM_COLOR;

            // 备注列可以编辑
            if(colpos + colcount - 1 == j && i < rowpos)
                flag = Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsDragEnabled|Qt::ItemIsDropEnabled|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled;
            else
                flag = Qt::ItemIsEnabled;

            setScheduleItemStyle(pItem, flag, QBrush(Qt::black), backgroundBrush, QFont("Microsoft YaHei", 11));
        }
    }

    return;
}

void MyScheduleTable::setScheduleItemStyle(QTableWidgetItem *pItem, Qt::ItemFlags flag, QBrush frontColor, QBrush backColor, QFont itemFont)
{
    if(NULL == pItem){
        QLOG_WARN() << "NULL == pItem";
        return;
    }

    // 文字居中
    pItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

    // 不可编辑
    pItem->setFlags(flag);

    // 设置字体颜色
    pItem->setForeground(frontColor);

    // 设置背景颜色
    pItem->setBackground(backColor);

    // 设置字体格式
    pItem->setFont(itemFont);

    return;
}

void MyScheduleTable::paintScheduleItem(int i, int j, QString text, QColor color)
{
    QBrush bsh;
    QTableWidgetItem *pItem = item(i , j);
    if(NULL == pItem){
        QLOG_WARN() << "NULL == pItem";
        return;
    }

    pItem->setText(text);
    pItem->setBackground(color);

    return;
}

void MyScheduleTable::createActions(int daycount, int membercount)
{
    // 允许QTableWidget接收QWidget::customContextMenuRequested()信号。
    setContextMenuPolicy(Qt::CustomContextMenu);

    this->daycount = daycount;
    this->membercount = membercount;

    //创建菜单项
     pMenu = new QMenu(this);
     QAction *pNone = new QAction("更改本日班次为空", this);
     QAction *pDay = new QAction("更改本日班次为白班", this);
     QAction *pNight = new QAction("更改本日班次为夜班", this);

     pMenu->addAction(pNone);
     pMenu->addAction(pDay);
     pMenu->addAction(pNight);

     // 监听action的triggered信号，将参数pos发送至schedule
     connect(pNone, &QAction::triggered, this, [=](){emit signal_changeScheduleWorkAction_triggered(menuPos, DATESET_NONE);});
     connect(pDay, &QAction::triggered, this,  [=](){emit signal_changeScheduleWorkAction_triggered(menuPos, DATESET_DAYWORK);});
     connect(pNight, &QAction::triggered, this, [=](){emit signal_changeScheduleWorkAction_triggered(menuPos, DATESET_NIGHTWORK);});

    // 监听QWidget::customContextMenuRequested信号
    connect(this, &QWidget::customContextMenuRequested, this, &MyScheduleTable::slot_menu_customContextMenuRequested);

    return ;
}

void MyScheduleTable::changeScheduleItem(QTableWidgetItem *pItem, int type)
{
    if( NULL  == pItem){
        QLOG_WARN() << "getItemRowAndColFromPos err";
        return;
    }

    QString str;
    QBrush bsh;

    if(DATESET_NONE == type){
        str = "";
        bsh =  QBrush(QColor(DATESET_TABLE_WORKDAY_COLOR));
    }
    else if(DATESET_DAYWORK == type){
        str = "白";
        bsh =  QBrush(QColor(SCHEDULE_TABLE_DAY_ITEM_COLOR));
    }
    else if(DATESET_NIGHTWORK == type){
        str = "夜";
        bsh =  QBrush(QColor(SCHEDULE_TABLE_NIGHT_ITEM_COLOR));
    }

    pItem->setText(str);
    pItem->setBackground(bsh);

    return;
}

void MyScheduleTable::slot_menu_customContextMenuRequested(const QPoint &pos)
{
    int row = 0, col = 0;
    if( NULL  == this->getItemRowAndColFromPos(pos, row, col)){
        QLOG_WARN() << "getItemRowAndColFromPos err";
        return;
    }

    if(row >= daycount || col >= membercount)   return;

    menuPos = pos;
    pMenu->exec(QCursor::pos());
    return;
}
